package com.itheima.edu.info.subwaymanager.controller;

import com.itheima.edu.info.subwaymanager.domain.Manager;

import java.io.*;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @Author 王加鑫
 * @Date 2021/5/21 20:52
 * @Version 1.0
 */
public class LoginControllerImpl implements LoginController {

    private static Properties properties = new Properties();

    private Scanner sc = new Scanner(System.in);

    //请用户输入账号密码进行登录系统
    @Override
    public void login() {
        while (true){
            System.out.println("请输入工号");
            String id = sc.next();
            //判断账号是否存在
            boolean hasId = hasId(id);
            //如果账号不存在或者容器为空则让用户开始操作
            if(!hasId){
                System.out.println("工号不存在哦,重新输入工号还是注册一个账号(1是注册一个新账号 2是重新登录 3是退出系统)");
                String choice = sc.next();
                lo:switch (choice){
                    case "1":
                        new RegisterControllerImpl().register();
                        System.out.println("注册成功哦,开始登录嘀嘀嘀---------------------------");
                        break lo;
                    case "2":
                        login();
                        return;
                    case "3":
                        System.exit(0);
                    default:
                        System.out.println("您的输入有误!!!!");
                }
            }else { //账户存在再对密码进行校验
                System.out.println("请输入姓名");
                String name = sc.next();
                boolean hasName = hasName(name);
                if(!hasName){
                    System.out.println("密码错误,请重新输入");
                }else {
                    System.out.println("登录成功!开始操作本系统!!!");
                    return;
                }
            }
        }
    }

    //判断本地数据库是否有此账号
    @Override
    public boolean hasId(String id) {
        ObjectInputStream ois = null;
        AtomicBoolean flag = new AtomicBoolean(false);
        try {
            ois = new ObjectInputStream(new FileInputStream("itheima-edu-info- subwaymanager/managers.txt"));
            //如果本次用户输入的账号不存在,请用户跳转到注册界面
            ArrayList<Manager> managers = (ArrayList<Manager>) ois.readObject();
            if(managers.size() != 0){
                managers.forEach((manager) -> {
                    String mId = manager.getId();
                    if(id.equals(mId)){
                        //匿名内部类或者lambda表达式不能直接使用"="给外部变量赋值
                        flag.set(true);
                    }
                } );
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return flag.get();
    }

    //对已有账号进行校验密码
    @Override
    public boolean hasName(String name) {
        ObjectInputStream ois = null;
        AtomicBoolean flag = new AtomicBoolean(false);
        try {
            ois = new ObjectInputStream(new FileInputStream("itheima-edu-info- subwaymanager/managers.txt"));
            //如果本次用户输入的账号不存在,请用户跳转到注册界面
            ArrayList<Manager> managers = (ArrayList<Manager>) ois.readObject();
            if(managers.size() != 0){
                managers.forEach((manager) -> {
                    String mName = manager.getName();
                    if(name.equals(mName)){
                        flag.set(true);
                    }
                } );
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return flag.get();
    }
}
